import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.telkomsigma.spring.hibernate.persistence.domain.FootballTeam;
import com.telkomsigma.spring.hibernate.persistence.domain.Photo;
import com.telkomsigma.spring.hibernate.persistence.domain.Player;
import com.telkomsigma.spring.hibernate.persistence.domain.Reserve;
import com.telkomsigma.spring.hibernate.persistence.domain.TPlayer;
import com.telkomsigma.spring.hibernate.service.PlayerService;
import com.telkomsigma.spring.hibernate.service.TeamService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = false)
@Transactional
public class TesServicePlayer {

	Log log = LogFactory.getLog(this.getClass());
	
	@Autowired
	PlayerService playerDao;
	
	@Autowired
	TeamService teamDao;
	
	@Test
	public void findAllPlayer(){
		List<TPlayer> list = playerDao.findAll(0);
		Assert.assertNotNull(list);
		System.out.println(list);
		for(TPlayer player : list) {
			log.info(player.getFirstName());
		}
	}
	
	@Test
	public void insertPlayer() throws IOException {
		FootballTeam team = teamDao.findById(1);
		File file = new File("/media/ANAK KUCING/dua/adi/fd/So Nyuh Shi Dae/20090610004404.jpg");
		InputStream is = new BufferedInputStream(new FileInputStream(file));
		Blob blob = Hibernate.createBlob(is);
		
		Photo photo = new Photo();
		photo.setId(124);
		photo.setData(blob);
		
		Player player = new Player();
		player.setFirstName("Yahya");
		player.setLastName("Zainiss");
		player.setJoinDate(new Date());
		player.setSalary(500000d);
		player.setFootballTeam(team);
		
		photo.setPlayer(player);
		
		player.setPhoto(photo);
		
		Assert.assertNotNull(playerDao.insert(player));
	}
	
	@Test
	public void findById(){
		TPlayer player = playerDao.findById(1);
		Assert.assertNotNull(player);
	}
	
	@Test
	public void updatePlayer() {
		TPlayer player = playerDao.findById(122);
		
		if(player instanceof Player) {
			Player player2 = (Player) player; 
			player2.setFirstName("Yahya");
			player2.setLastName("Zainissdddd");
			player2.setJoinDate(new Date());
			player2.setSalary(5000d);
			
			Assert.assertNotNull(playerDao.update(player2));
		}else{
			Reserve reserve = (Reserve) player;
			reserve.setFirstName("Yahya");
			reserve.setLastName("Zainissdddd");
			reserve.setJoinDate(new Date());
			reserve.setCommission(50500d);
			
			Assert.assertNotNull(playerDao.update(reserve));
		}
		

	}
	
	@Test
	public void removePlayer() {
		TPlayer player = playerDao.findById(122);
		Assert.assertTrue(playerDao.remove(player));
	}
	
	@Test
	public void removeListPlayer(){
		TPlayer player = playerDao.findById(122);
		TPlayer player2 = playerDao.findById(123);
		List<TPlayer> list = new ArrayList<TPlayer>();
		list.add(player);
		list.add(player2);
		Assert.assertTrue(playerDao.removeList(list));
	}
}
